package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/17 - 14:08
 */
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;
    @Override
    public PageInfo<Process> queryProcessListByPageAndCondition(String processCode, String processName, Integer isActive, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Process> processList = processMapper.queryProcessListByPageAndCondition(processCode, processName, isActive, page, size);
        PageInfo<Process> processPageInfo = new PageInfo<>(processList);
        return processPageInfo;
    }

    @Override
    public int addProcess(Process process) {
        Process process1 = processMapper.queryProcessByProcessCode(process.getProcessCode());
        if (process1 != null) {
            return -1;
        }
       int result =  processMapper.addProcess(process);
       return  result;
    }

    @Override
    public Process queryProcessByProcessCode(String processCode) {
        return processMapper.queryProcessByProcessCode(processCode);
    }

    @Override
    public int updateProcessById(Process process) {
        Process process1 = processMapper.queryProcessByProcessCode(process.getProcessCode());
        if(process1.getProcessCode().equals(process.getProcessCode())){
            return  processMapper.updateProcessById(process);
        }
        if (process1 != null) {
            return -1;
        }
        return processMapper.updateProcessById(process);

    }

    @Override
    public int deleteProcessById(Integer id) {
        int result =  processMapper.deleteProcessById(id);
        return result;
    }

    //根据id批量删除工序
    @Override
    public int deleteProcessByListIds(List<Integer> ids) {
        int successCount = 0;

        for (Integer id : ids) {
            // 调用 Mapper 方法，如果删除成功，会返回 1
            int result = processMapper.deleteProcessById(id);
            if (result == 1) {
                successCount++;
            }
        }

        if (successCount == ids.size()) {
            // 全部成功
            return 1;
        } else if (successCount > 0) {
            // 部分成功
            return -1;
        } else {
            // 全部失败
            return 0;
        }


    }
}
